package ModelImplementation

import (
	"salary-paymment/Model"
	"math"
	"time"
)

type HourlyClassification struct {
	itsRate float64
	itsTimeCards map[time.Time]TimeCard
}

func NewHourlyClassification(hourlyRate float64) *HourlyClassification {
	return &HourlyClassification{
		itsRate : hourlyRate,
	}
}

func (h *HourlyClassification) GetRate() float64 {
	return h.itsRate
}

func (h *HourlyClassification) GetTimeCards(date time.Time) TimeCard {
	return h.itsTimeCards[date]
}

func (h *HourlyClassification)AddTimeCard(tc TimeCard)  {
	h.itsTimeCards[tc.GetDate()] = tc
}

func (h *HourlyClassification) CalculatePayForTimeCard(tc TimeCard) float64 {
	hours := tc.GetHours()
	overtime := math.Max(0.0,hours-8.0)
	straightTime := hours - overtime
	return straightTime * h.itsRate + overtime * h.itsRate * 1.5
}

//实现PaymentClassification中的CalculatePay方法
func (h *HourlyClassification) CalculatePay(pc Model.Paycheck) float64 {
	totalPay := 0.0
	//payPeriodEndDate := pc.GetPayPeriodEndDate()

	for key, value := range h.itsTimeCards {
		dt1 := pc.GetPayPeriodStartDate()
		dt2 := pc.GetPayPeriodEndDate()

		if key.Before(dt1) && key.After(dt2) {
			totalPay += h.CalculatePayForTimeCard(value)
		}
	}

	return totalPay
}